package com.ruiyi.adrobot.service.accountinfo.impl;

import com.ruiyi.adrobot.beans.AccountInfo;
import com.ruiyi.adrobot.beans.Robot;
import com.ruiyi.adrobot.mongo.accountInfo.AccountInfoRepository;
import com.ruiyi.adrobot.service.accountinfo.AccountInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by tao.zeng on 2017/7/18.
 */
@Service
public class AccountInfoServiceImpl implements AccountInfoService {
    @Autowired
    private AccountInfoRepository accountInfoRepository;

    @Override
    public AccountInfo.Account findRandomAccount(Integer fun, Boolean virtual, String startDate, String endDate, List<String> usedImeis, List<Robot> canExcuteRobots) {
        AccountInfo.Account account = null;
        for (Robot robot : canExcuteRobots) {
            account = accountInfoRepository.findRandomAccount(fun, robot.getModel(), virtual, startDate, endDate, usedImeis);
            if (null != account) {
                account.setExcuteSimCode(robot.getSimCode());
                break;
            }
        }
        if (null == account) {
            account = accountInfoRepository.findRandomAccount(fun, null, virtual, startDate, endDate, usedImeis);
            if (null == account) {
                return null;
            }
            account.setExcuteSimCode(canExcuteRobots.get(0).getSimCode());
        }
        return account;
    }

    @Override
    public AccountInfo findOne(Integer fun) {
        return accountInfoRepository.findOne(fun);
    }

    @Override
    public void appendAccount(Integer fun, AccountInfo.Account account) {
        accountInfoRepository.appendAccount(fun, account);
    }
}
